﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Data;
using System.Windows;
using System.IO;
using System.Windows.Markup;
using System.Windows.Input;
using System.Windows.Media;

namespace elr_tracking_wpf.UI
{
    public class UIGrid
    {
        public ScrollViewer PanelCentral;
        public StackPanel sPanel;

        private pom _despliega;

        public pom Despliega1
        {
            get { return _despliega; }
            set { _despliega = value; }
        }

        public UIGrid(ScrollViewer PC)
        {
            PanelCentral = PC;
            // --------------------- Grid   -----------------------------------
            string nombre3 = @".../../ControlGridEntradas.xaml";
            DependencyObject rootElement3;
            using (FileStream fs3 = new FileStream(nombre3, FileMode.Open))
            {
                rootElement3 = (DependencyObject)XamlReader.Load(fs3);
            }
            PanelCentral.Content = rootElement3;
            FrameworkElement frameworkElement3 = (FrameworkElement)rootElement3;
            sPanel = (StackPanel)frameworkElement3.FindName("SPnlSolicitudes");

            DataTable dtGrid = CAT.CargaGrid.GetDataGrid();
            CargaGrid(dtGrid);
        }
        private void CargaGrid(DataTable dt)
        {
            if (sPanel.Children.Count > 0)
                sPanel.Children.RemoveAt(0); //Elimina el grid Anterior
            Grid dtg = new Grid();
            dtg.Name = "dtgSol";
            DefineColumnasGrid(dtg);
            DefineHeader(dtg);
            CargaRenglones(dtg, dt);
            sPanel.Children.Add(dtg);
        }
        private void DefineColumnasGrid(Grid dGrid)
        {
            ColumnDefinition col1 = new ColumnDefinition();
            col1.Name = "CheckBox";
            col1.Width = new GridLength(25);

            ColumnDefinition col2 = new ColumnDefinition();
            col2.Name = "FACTURA";
            col2.Width = new GridLength(180);

            ColumnDefinition col3 = new ColumnDefinition();
            col3.Name = "PROVEEDOR";
            col3.Width = new GridLength(180);

            ColumnDefinition col4 = new ColumnDefinition();
            col4.Name = "LOTE";
            col4.Width = new GridLength(180);

            ColumnDefinition col5 = new ColumnDefinition();
            col5.Name = "PEDIMENTO";
            col5.Width = new GridLength(180);

            ColumnDefinition col6 = new ColumnDefinition();
            col6.Name = "ID_TIPO_PEDIMENTO";
            col6.Width = new GridLength(180);

            ColumnDefinition col7 = new ColumnDefinition();
            col7.Name = "CANTIDAD";
            col7.Width = new GridLength(180);

            //DataGridLength(1, DataGridLengthUnitType.SizeToCells);
            dGrid.ColumnDefinitions.Add(col1);
            dGrid.ColumnDefinitions.Add(col2);
            dGrid.ColumnDefinitions.Add(col3);
            dGrid.ColumnDefinitions.Add(col4);
            dGrid.ColumnDefinitions.Add(col5);
            dGrid.ColumnDefinitions.Add(col6);
            dGrid.ColumnDefinitions.Add(col7);
        }
        private void DefineHeader(Grid dGrid)
        {
            //Agrega renglon de Encabezado
            RowDefinition row1 = new RowDefinition();
            row1.Name = "Header";
            dGrid.RowDefinitions.Add(row1);

            //Agrega texto a encabezado
            TextBlock txt1 = new TextBlock();
            txt1.Text = "";
            txt1.VerticalAlignment = VerticalAlignment.Center;
            txt1.HorizontalAlignment = HorizontalAlignment.Center;
            Grid.SetColumn(txt1, 0);
            Grid.SetRow(txt1, 0);

            TextBlock txt2 = new TextBlock();
            txt2.Text = "Factura";
            txt2.VerticalAlignment = VerticalAlignment.Center;
            txt2.HorizontalAlignment = HorizontalAlignment.Center;
            txt2.FontWeight = FontWeights.UltraBold;
            Grid.SetColumn(txt2, 1);
            Grid.SetRow(txt2, 0);

            TextBlock txt3 = new TextBlock();
            txt3.Text = "POM";
            txt3.VerticalAlignment = VerticalAlignment.Center;
            txt3.HorizontalAlignment = HorizontalAlignment.Center;
            txt3.FontWeight = FontWeights.UltraBold;
            Grid.SetColumn(txt3, 2);
            Grid.SetRow(txt3, 0);

            TextBlock txt4 = new TextBlock();
            txt4.Text = "Lote";
            txt4.VerticalAlignment = VerticalAlignment.Center;
            txt4.HorizontalAlignment = HorizontalAlignment.Center;
            txt4.FontWeight = FontWeights.UltraBold;
            txt4.FontSize = 14;
            Grid.SetColumn(txt4, 3);
            Grid.SetRow(txt4, 0);

            TextBlock txt5 = new TextBlock();
            txt5.Text = "Pedimento";
            txt5.VerticalAlignment = VerticalAlignment.Center;
            txt5.HorizontalAlignment = HorizontalAlignment.Center;
            txt5.FontWeight = FontWeights.UltraBold;
            txt5.FontSize = 14;
            Grid.SetColumn(txt5, 4);
            Grid.SetRow(txt5, 0);

            TextBlock txt6 = new TextBlock();
            txt6.Text = "Tipo de Pedimento";
            txt6.VerticalAlignment = VerticalAlignment.Center;
            txt6.HorizontalAlignment = HorizontalAlignment.Center;
            txt6.FontWeight = FontWeights.UltraBold;
            txt6.FontSize = 14;
            Grid.SetColumn(txt6, 5);
            Grid.SetRow(txt6, 0);

            TextBlock txt7 = new TextBlock();
            txt7.Text = "Cantidad";
            txt7.VerticalAlignment = VerticalAlignment.Center;
            txt7.HorizontalAlignment = HorizontalAlignment.Center;
            txt7.FontWeight = FontWeights.UltraBold;
            txt7.FontSize = 14;
            Grid.SetColumn(txt7, 6);
            Grid.SetRow(txt7, 0);

            dGrid.Children.Add(txt1);
            dGrid.Children.Add(txt2);
            dGrid.Children.Add(txt3);
            dGrid.Children.Add(txt4);
            dGrid.Children.Add(txt5);
            dGrid.Children.Add(txt6);
            dGrid.Children.Add(txt7);
        }
        private void CargaRenglones(Grid dGrid, DataTable dtGrid)
        {
            DefineRenglones(dGrid, "Header");

            for (int x = 0; x < dtGrid.Rows.Count; x++)
            {
                DefineRenglones(dGrid, "ID" + dtGrid.Rows[x]["ID_LOTE"].ToString());
                DefineEstiloRenglones(dGrid, x + 1, 0);

                for (int y = 0; y < dtGrid.Columns.Count; y++)
                {
                    if (y == 0)
                        DefineCheckBox(dGrid, x + 1, 0, "ID" + dtGrid.Rows[x]["ID_LOTE"].ToString());
                    else
                        DefineTextoCeldas(dGrid, x + 1, y, dtGrid.Rows[x][y].ToString());
                }
                DefineEventoRenglones(dGrid, x + 1, 1, "ID" + dtGrid.Rows[x]["ID_LOTE"].ToString());
            }
        }
        private void DefineTextoCeldas(Grid dGrid, int x, int y, string Texto)
        {
            TextBlock stp = new TextBlock();
            stp.Text = Texto;
            stp.VerticalAlignment = VerticalAlignment.Center;
            stp.HorizontalAlignment = HorizontalAlignment.Center;
            Grid.SetColumn(stp, y);
            Grid.SetRow(stp, x);
            dGrid.Children.Add(stp);
        }
        private void DefineRenglones(Grid dGrid, String Name)
        {
            RowDefinition row1 = new RowDefinition();
            row1.Name = Name;
            row1.Height = new GridLength(30);
            //if (Name != "Header")
            //row1.Height = new GridLength(30);
            dGrid.RowDefinitions.Add(row1);
        }
        /// <summary>
        /// Estilo de los renglones 
        /// </summary>
        /// <param name="dGrid"></param>
        /// <param name="x"></param>
        /// <param name="y"></param>
        private void DefineEstiloRenglones(Grid dGrid, int x, int y)
        {
            //Fondo del renglon
            Border bor = new Border();
            bor.Background = System.Windows.Media.Brushes.LightGray;
            bor.CornerRadius = new CornerRadius(0);
            bor.Margin = new Thickness(0);
            
            Grid.SetColumn(bor, y); 
            Grid.SetRow(bor, x);
            Grid.SetColumnSpan(bor, 7);
            dGrid.Children.Add(bor);

        }
        private void DefineCheckBox(Grid dGrid, int x, int y, string Texto)
        {
            CheckBox chk = new CheckBox();
            chk.Name = Texto;
            chk.HorizontalAlignment = HorizontalAlignment.Center;
            chk.VerticalAlignment = VerticalAlignment.Center;
            Grid.SetColumn(chk, y);
            Grid.SetRow(chk, x);
            dGrid.Children.Add(chk);
        }
        private void DefineEventoRenglones(Grid dGrid, int x, int y, String Name)
        {
            Border bor = new Border();
            bor.Background = System.Windows.Media.Brushes.Transparent;
            bor.CornerRadius = new CornerRadius(0, 5, 5, 0);
            bor.Margin = new Thickness(3);
            bor.Name = Name;
            bor.MouseDown += new MouseButtonEventHandler(dgrw_MouseUp); 
            bor.MouseEnter += new MouseEventHandler(bor_MouseEnter);
            bor.MouseLeave += new MouseEventHandler(bor_MouseLeave);
            Grid.SetColumn(bor, y);
            Grid.SetRow(bor, x);
            Grid.SetColumnSpan(bor, 7);
            dGrid.Children.Add(bor);
            Border bor1 = new Border();
            bor1.Background = System.Windows.Media.Brushes.Transparent;
            bor1.CornerRadius = new CornerRadius(5, 0, 0, 5);
            bor1.Margin = new Thickness(3, 3, -3, 3);
            bor1.Name = "chk" + Name;
            bor1.MouseDown += new MouseButtonEventHandler(bor_mouseClick);
            bor1.MouseEnter += new MouseEventHandler(bor_MouseEnter);
            bor1.MouseLeave += new MouseEventHandler(bor_MouseLeave);
            Grid.SetColumn(bor1, 0);
            Grid.SetRow(bor1, x);
            dGrid.Children.Add(bor1);
        }
        void dgrw_MouseUp(object sender, MouseButtonEventArgs e)
        {
            RowDefinition actual = (RowDefinition)sender;
            string a = actual.Name.ToString();
            try
            {
                
                MainWindow test = new MainWindow();
                test.svContainer.Content = null;
                MessageBox.Show(a);
                test.svContainer.Content = Despliega1;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        /// <summary>
        /// Método acción de Hover (cuando el mouse pasa sobre la fila)
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void bor_MouseEnter(object sender, MouseEventArgs e)
        {
            Border renglon;
            Border renglon1 = new Border(); ;
            string name;
            try
            {
                renglon = (Border)sender;
                name = renglon.Name.StartsWith("chk") ? renglon.Name.Substring(3, renglon.Name.Length - 3) : "chk" + renglon.Name;

                foreach (object o in ((Grid)(renglon.Parent)).Children)
                    if (o.GetType() == typeof(Border))
                        if (((Border)o).Name == name)
                            renglon1 = (Border)o;
                renglon.Background = new SolidColorBrush(Colors.Azure);
                renglon.Opacity = 0.5;
                renglon1.Background = new SolidColorBrush(Colors.Azure);
                renglon1.Opacity = 0.5;
                renglon.Cursor = Cursors.Hand;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void bor_MouseLeave(object sender, MouseEventArgs e)
        {
            Border renglon;
            Border renglon1 = new Border(); ;
            string name;
            try
            {
                renglon = (Border)sender;
                name = renglon.Name.StartsWith("chk") ? renglon.Name.Substring(3, renglon.Name.Length - 3) : "chk" + renglon.Name;

                foreach (object o in ((Grid)(renglon.Parent)).Children)
                    if (o.GetType() == typeof(Border))
                        if (((Border)o).Name == name)
                            renglon1 = (Border)o;
                renglon.Background = new SolidColorBrush(Colors.Transparent);
                renglon.Opacity = 1.0;
                renglon1.Background = new SolidColorBrush(Colors.Transparent);
                renglon1.Opacity = 1.0;
                renglon.Cursor = Cursors.Hand;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        void bor_mouseClick(object sender, MouseButtonEventArgs e)
        {
            CheckBox chk1 = null;
            string name;
            try
            {
                Border brdr1 = (Border)sender;
                name = brdr1.Name.Substring(3, brdr1.Name.Length - 3);
                foreach (object o in ((Grid)(brdr1.Parent)).Children)
                    if (o.GetType() == typeof(CheckBox))
                        if (((CheckBox)o).Name == name)
                            chk1 = (CheckBox)o;
                if (chk1 != null)
                    chk1.IsChecked = !chk1.IsChecked;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
